package com.yy.jhl.ui.user.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.yy.common.base.BaseActivity;
import com.yy.common.commonutils.FileUtil;
import com.yy.common.commonutils.SPUtils;
import com.yy.common.commonutils.StringUtil;
import com.yy.common.commonwidget.OnNoDoubleClickListener;
import com.yy.jhl.R;
import com.yy.jhl.app.AppConstant;
import com.yy.jhl.bean.LoginInfo;
import com.yy.jhl.ui.main.activity.MainActivity;
import com.yy.jhl.ui.user.contract.LoginContract;
import com.yy.jhl.ui.user.presenter.LoginPresenter;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by libingn on 2017/12/6.
 */

public class LoginActivity extends BaseActivity<LoginPresenter> implements LoginContract.View {
    @BindView(R.id.user_name_edit)
    EditText userNameEdit;
    @BindView(R.id.pwd_edit)
    EditText pwdEdit;
    @BindView(R.id.ensure_btn)
    Button ensureBtn;
    @BindView(R.id.find_pwd_text)
    TextView findPwdText;
    @BindView(R.id.register_text)
    TextView registerText;
    @BindView(R.id.app_logo)
    ImageView appLogo;
    private int loginType = 1;//1:供应商 2：采购商

    public static void startAction(Context mContext, int loginType) {
        Intent intent = new Intent(mContext, LoginActivity.class);
        intent.putExtra("loginType", loginType);
        mContext.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.act_login;
    }

    @Override
    public void initView() {
        if ( SPUtils.getSharedIntData(mContext, AppConstant.SP_KEY_LOGIN_TYPE) != 3){
            userNameEdit.setText(SPUtils.getSharedStringData(mContext,AppConstant.SP_KEY_USER_NAME));
            pwdEdit.setText(SPUtils.getSharedStringData(mContext,AppConstant.SP_KEY_USER_PWD));
        }
        loginType = getIntent().getIntExtra("loginType", 1);
        showTitle(loginType == 1 ? "供应商登录" : "采购商登录", new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                finish();
                startActivity(LoginSelectActivity.class);
            }
        });
        if (loginType == 1) {
            registerText.setVisibility(View.VISIBLE);
        }
        //保存登录类型
        SPUtils.setSharedIntData(mContext, AppConstant.SP_KEY_LOGIN_TYPE, loginType);
    }

    @OnClick({R.id.ensure_btn, R.id.find_pwd_text, R.id.register_text})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ensure_btn:
                String userName = userNameEdit.getText().toString();
                String pwd = pwdEdit.getText().toString();
                if (StringUtil.isEmpty(userName)) {
                    showShortToast("用户名不能为空");
                    return;
                }
                if (StringUtil.isEmpty(pwd)) {
                    showShortToast("密码不能为空");
                    return;
                }
                if (pwd.length() < 6 || pwd.length() > 30) {
                    showShortToast("请输入6-30位的密码");
                    return;
                }
                mPresenter.login(userName, pwd, loginType, "10000");
//                appLogo.setImageBitmap(FileUtil.generateBitmap("111111",400,400));;
                break;
            case R.id.find_pwd_text:
                FindPwdActivity.startAction(mContext, loginType);
                break;
            case R.id.register_text:
                startActivity(RegisterActivity.class);
                break;
        }
    }

    @Override
    public void loginSuccess(LoginInfo loginInfo) {
        SPUtils.setSharedStringData(mContext, AppConstant.SP_KEY_USER_NAME, userNameEdit.getText().toString());
        SPUtils.setSharedStringData(mContext, AppConstant.SP_KEY_USER_PWD, pwdEdit.getText().toString());
        SPUtils.setSharedStringData(mContext, AppConstant.SP_KEY_TOKEN, loginInfo.getToken());
        SPUtils.setSharedObjectData(mContext, AppConstant.SP_KEY_USER_INFO, loginInfo.getUser());
        startActivity(MainActivity.class);
        finish();
    }

    @Override
    public void loginFail() {

    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN
                && event.getRepeatCount() == 0) {
            finish();
            startActivity(LoginSelectActivity.class);
            return false;
        } else {
            return super.dispatchKeyEvent(event);
        }
    }
}
